<?php

/**
 * atributos actions.
 *
 * @package    auip
 * @subpackage atributos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class atributosActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
        $this->atributos_adicionales_becas = Doctrine_Core::getTable('AtributosAdicionalesBeca')->createQuery('a')->execute();
  }
  
  public function executeLista(sfWebRequest $request)
  {
	  $this->becas = Doctrine_Core::getTable('Beca')->createQuery('b')->where('b.registro_activo <> 0')->addOrderBy('b.fecha_inicio DESC')->execute();
  }
  
  
    public function executeTerminar(sfWebRequest $request) 
    {        
		//echo '-->'.$request->getParameter('beca_id');
        $this->redirect('becas/show?id='.$this->getUser()->getAttribute('beca_id'));        
    }

public function executeShow(sfWebRequest $request)
  { 
    $this->beca = Doctrine_Core::getTable('Beca')->find(array($request->getParameter('beca_id')));       
    $this->forward404Unless($this->beca);
  }

  public function executeNew(sfWebRequest $request)
  { 
    $this->form = new AtributosAdicionalesBecaForm();
    
     if($request->getParameter('beca_id') != '')
        {
				$this->getUser()->setAttribute('beca_id', $request->getParameter('beca_id'));
				$this->getUser()->setAttribute('nom', $request->getParameter('nom'));
				$this->form->setDefault('beca_id', $request->getParameter('beca_id'));    
				$this->form->setDefault('Nombre_de_la_Beca', $request->getParameter('nom')); 
		}
		else
		{
				$this->form->setDefault('beca_id', $this->getUser()->getAttribute('beca_id'));    
				$this->form->setDefault('Nombre_de_la_Beca', $this->getUser()->getAttribute('nom'));

		}
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    $nombrebeca = $request->getParameter('nom');
    
    $this->form = new AtributosAdicionalesBecaForm();

    $this->processForm($request, $this->form, $nombrebeca);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
      $id_beca = $request->getParameter('beca_id');
      $id_atributo = $request->getParameter('atributo_id');      
      if ( ($id_beca > 0) && ($id_atributo == 'no') )
      {
           $this->redirect('becas/edit?id='.$id_beca);          
      }      
      else
      {
		$this->getUser()->setAttribute('beca_id',$request->getParameter('beca_id'));    
		$this->getUser()->setAttribute('atributo_id',$request->getParameter('atributo_id'));
		$this->getUser()->setAttribute('nombre_de_la_beca',$request->getParameter('nombre_beca'));
		
        $this->forward404Unless($atributos_adicionales_beca = Doctrine_Core::getTable('AtributosAdicionalesBeca')->find(array($request->getParameter('beca_id'), $request->getParameter('atributo_id'))), sprintf('Object atributos_adicionales_beca does not exist (%s).', $request->getParameter('beca_id'), $request->getParameter('atributo_id')));
        $atributos_adicionales_beca->setAtributoId($id_atributo);
        $atributos_adicionales_beca->setBecaId($id_beca);        
        $this->form = new AtributosAdicionalesBecaForm($atributos_adicionales_beca);
        $this->form->setDefault('Nombre_de_la_Beca', $request->getParameter('nombre_beca'));
      }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($atributos_adicionales_beca = Doctrine_Core::getTable('AtributosAdicionalesBeca')->find(array($request->getParameter('beca_id'),
$request->getParameter('atributo_id'))), sprintf('Object atributos_adicionales_beca does not exist (%s).', $request->getParameter('beca_id'),
$request->getParameter('atributo_id')));
    $this->form = new AtributosAdicionalesBecaForm($atributos_adicionales_beca);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($atributos_adicionales_beca = Doctrine_Core::getTable('AtributosAdicionalesBeca')->find(array($request->getParameter('beca_id'),
    $request->getParameter('atributo_id'))), sprintf('Object atributos_adicionales_beca does not exist (%s).', $request->getParameter('beca_id'),
    $request->getParameter('atributo_id')));
    $atributos_adicionales_beca->delete();

    $this->redirect('atributos/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form, $nombrebeca)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {        
        $atributos_adicionales_beca = $form->save();
        $this->getUser()->setAttribute('msg', 'Se ha ADICIONADO un NUEVO item a la convocatoria codigo '.$this->getUser()->getAttribute('beca_id'));
        $this->redirect('atributos/new');
    }
  }
}
